'Copyright (C) 2007 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Windows.Forms

Public Class Blitz
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String
            Dim gamingDownload As Single
            Dim VoIPDownload As Single
            Dim pipeDownload As Single
            Dim newsDownload As Single
            Dim newsExcess As Single

            'strHTMLData = My.Computer.FileSystem.ReadAllText("C:\programming\isp\blitz\blitznew.htm", System.Text.Encoding.ASCII)

            'login
            Dim POSTData As String = "edit[name]=" & ConfigurationData.Username & "&edit[pass]=" & ConfigurationData.Password
            strHTMLData = ISPPost("https://members.blitzip.com.au/user/login?destination=blog%2F1", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strHTMLData.Contains("Incorrect username or password") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
            Else
                'account list
                Dim TempData As String = ISPGet("https://members.blitzip.com.au/broadband/custusage", CookieContainer, System.Net.CredentialCache.DefaultCredentials, "https://members.blitzip.com.au/broadband/custusage")
                '
                Dim PlanID As String = ExtractText(TempData, "id=""edit-dsl_id""><option value", 0, "=", ">").Replace("""", "")
                If PlanID = "" Then Exit Sub
                Dim TokenID As String = ExtractText(TempData, "[token]"" value", 0, "=""", """").Replace("""", "")

                'usage
                If PlanID <> "" Then
                    POSTData = "edit[dsl_id]=" & PlanID & "&edit[token]=" & TokenID & "&op=Select"
                    strHTMLData = ISPPost("https://members.blitzip.com.au/broadband/custusage", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

                    If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

                    'logout
                    ISPGet("https://members.blitzip.com.au/logout", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

                    '##########################################################

                    'process response
                    ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "Monthly Peak Usage:", 0, "<b>", " "))) * 1000
                    ISPReturnData.OffpeakDownload = CSng(Val(ExtractText(strHTMLData, "Monthly Off-Peak Usage:", 0, "<b>", " "))) * 1000

                    'get PIPE
                    pipeDownload = CSng(Val(ExtractText(strHTMLData, "<td>PIPE</td>", 0, "<td>", " "))) * 1000

                    'get News
                    newsDownload = CSng(Val(ExtractText(strHTMLData, "Usenet News Usage:", 0, "<td>", " "))) * 1000

                    'get News Excess
                    newsExcess = CSng(Val(ExtractText(strHTMLData, "Usenet News Excess:", 0, "<td>", " "))) * 1000

                    ' get others
                    gamingDownload = CSng(Val(ExtractText(strHTMLData, "<td>Koala Gaming Network</td>", 0, "<td>", " "))) * 1000
                    VoIPDownload = CSng(Val(ExtractText(strHTMLData, "<td>Koala VoIP</td>", 0, "<td>", " "))) * 1000

                    ISPReturnData.LastUpdated = CDate(ExtractText(strHTMLData, "Last Update Time:", 0, "<b>", "</b>"))
                    'Dim LastUpdated As String = ExtractText(strHTMLData, "Last Update Time:", 0, "<b>", "</b>")
                    'ISPReturnData.LastUpdated = Date.ParseExact(LastUpdated, "dd MMM yy hh:mmtt", Nothing)

                    '##########################################################

                    ProcessISPData(ConfigurationData, ISPReturnData)

                    'check for error
                    If ISPReturnData.ErrorMessage <> "" Then Exit Sub

                    '##########################################################

                    'create graph data

                    'peak
                    ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(ConfigurationData.PeakUsageLimit), ISPReturnData.PeakNow))

                    'offpeak
                    ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, 0, ISPReturnData.OffpeakNow))

                    'days elapsed
                    ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

                    '##########################################################

                    'create stats data
                    ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                    ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                    ISPReturnData.TextItemList.Add(New TextItem("Pipe:", pipeDownload & " MB"))
                    ISPReturnData.TextItemList.Add(New TextItem("News:", newsDownload & " MB"))
                    ISPReturnData.TextItemList.Add(New TextItem("News Excess:", newsExcess & " MB"))
                    ISPReturnData.TextItemList.Add(New TextItem("Koala Gaming:", gamingDownload & " MB"))
                    ISPReturnData.TextItemList.Add(New TextItem("Koala VoIP:", VoIPDownload & " MB"))

                    If ISPReturnData.ShapedNow = False Then
                        ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                    Else
                        ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                    End If
                End If

                '##########################################################

                'process alerts
                ProcessAlerts(ConfigurationData, ISPReturnData)
            End If
        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
